﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace WpfRoutedEvent_CustomRoutedEvent
{
    class TimeButton : Button
    {
        // 声明和注册路由事件
        public static readonly RoutedEvent ReportTimeEvent = 
            EventManager.RegisterRoutedEvent("ReportTime", RoutingStrategy.Tunnel, 
            typeof(EventHandler<ReportTimeEventArgs>), typeof(TimeButton));

        // CLR事件包装器
        public event RoutedEventHandler ReportTime
        {
            add { this.AddHandler(ReportTimeEvent, value);}
            remove { this.RemoveHandler(ReportTimeEvent, value); }
        }

        // 激发路由事件
        protected override void OnClick()
        {
            base.OnClick();

            ReportTimeEventArgs args = new ReportTimeEventArgs(ReportTimeEvent, this);
            args.ClickTime = DateTime.Now;
            this.RaiseEvent(args);
        }
    }
}
